From a72d3ac298bd453616f23b649a9de5ff31e50e26 Mon Sep 17 00:00:00 2001 From: Yiran-ASU Date: Tue, 8 Oct 2024 23:45:54 -0700 Subject: [PATCH 1/2] fixed the segmentation fault in CamemBERT and VIT --- experiments/CamemBERT/model/CamemBERT.py | 6 +++++- experiments/VIT/model/vit.py | 4 ++++ 2 files changed, 9 insertions(+), 1 deletion(-) diff --git a/experiments/CamemBERT/model/CamemBERT.py b/experiments/CamemBERT/model/CamemBERT.py index 1dd488f..1b5c28c 100644 --- a/experiments/CamemBERT/model/CamemBERT.py +++ b/experiments/CamemBERT/model/CamemBERT.py @@ -24,6 +24,7 @@ def __init__(self, model_name: str): self.layer3 = AutoModelForTokenClassification.from_pretrained(model_name).classifier def forward(self, input): + x = input.reshape(1, 7, 768) # Return only the logits. x = self.layer1(input).last_hidden_state x = self.layer2(x) @@ -44,7 +45,10 @@ def forward(self, input): print("Parsing sentence tokens.") example_input = prepare_sentence_tokens(model_name, sentence) -print(example_input.shape) +print("example_input shape: ", example_input.shape) + +example_input = example_input.reshape(1, 7*768) +print("example_input shape after reshaping: ", example_input.shape) print("Instantiating model.") model = OnlyLogitsHuggingFaceModel(model_name) diff --git a/experiments/VIT/model/vit.py b/experiments/VIT/model/vit.py index f9a37d9..b263f21 100644 --- a/experiments/VIT/model/vit.py +++ b/experiments/VIT/model/vit.py @@ -25,6 +25,7 @@ def __init__(self): self.layer2 = ViTForImageClassification.from_pretrained('google/vit-base-patch16-224').vit.layernorm self.layer3 = ViTForImageClassification.from_pretrained('google/vit-base-patch16-224').classifier def forward(self,x): + x = x.reshape(1, 197, 768) x = self.layer1(x).last_hidden_state x = self.layer2(x) x = self.layer3(x) @@ -37,6 +38,9 @@ def forward(self,x): model = prepare().eval() example_input = model(inputs) print(example_input.shape) +example_input = example_input.reshape(1, 197*768) +print("example_input shape after reshaping: ", example_input.shape) + vit_model = vit().eval() output = vit_model(example_input) From b29b27101c5bb5baf410515a588e57aba312b71f Mon Sep 17 00:00:00 2001 From: Yiran-ASU Date: Wed, 9 Oct 2024 09:29:33 -0700 Subject: [PATCH 2/2] fixed the segmentation fault in CamemBERT and VIT --- experiments/CamemBERT/model/CamemBERT.py | 3 ++- experiments/VIT/model/vit.py | 2 ++ 2 files changed, 4 insertions(+), 1 deletion(-) diff --git a/experiments/CamemBERT/model/CamemBERT.py b/experiments/CamemBERT/model/CamemBERT.py index 1b5c28c..1eb4998 100644 --- a/experiments/CamemBERT/model/CamemBERT.py +++ b/experiments/CamemBERT/model/CamemBERT.py @@ -26,7 +26,7 @@ def __init__(self, model_name: str): def forward(self, input): x = input.reshape(1, 7, 768) # Return only the logits. - x = self.layer1(input).last_hidden_state + x = self.layer1(x).last_hidden_state x = self.layer2(x) x = self.layer3(x) return x @@ -47,6 +47,7 @@ def forward(self, input): example_input = prepare_sentence_tokens(model_name, sentence) print("example_input shape: ", example_input.shape) +# The original example_input shape is [1, 7, 768], now we reshape it into [1, 7*768] example_input = example_input.reshape(1, 7*768) print("example_input shape after reshaping: ", example_input.shape) diff --git a/experiments/VIT/model/vit.py b/experiments/VIT/model/vit.py index b263f21..381a4c6 100644 --- a/experiments/VIT/model/vit.py +++ b/experiments/VIT/model/vit.py @@ -38,6 +38,8 @@ def forward(self,x): model = prepare().eval() example_input = model(inputs) print(example_input.shape) + +# The original example_input shape is [1, 197, 768], now we reshape it into [1, 197*768] example_input = example_input.reshape(1, 197*768) print("example_input shape after reshaping: ", example_input.shape)