fix(DPT,Depth-Anything) torch.export (#34103)
* Fix torch.export issue in dpt based models Signed-off-by: Phillip Kuznetsov <philkuz@gimletlabs.ai> * Simplify the if statements Signed-off-by: Phillip Kuznetsov <philkuz@gimletlabs.ai> * Move activation definitions of zoe_depth to init() Signed-off-by: Phillip Kuznetsov <philkuz@gimletlabs.ai> * Add test_export for dpt and zoedepth Signed-off-by: Phillip Kuznetsov <philkuz@gimletlabs.ai> * add depth anything Signed-off-by: Phillip Kuznetsov <philkuz@gimletlabs.ai> * Remove zoedepth non-automated zoedepth changes and zoedepth test Signed-off-by: Phillip Kuznetsov <philkuz@gimletlabs.ai> * [run_slow] dpt, depth_anything, zoedepth Signed-off-by: Phillip Kuznetsov <philkuz@gimletlabs.ai> --------- Signed-off-by: Phillip Kuznetsov <philkuz@gimletlabs.ai>
This commit is contained in:
committed by
GitHub
parent
9d16441e4f
commit
8cadf76e1c
@@ -18,6 +18,7 @@ import unittest
|
||||
|
||||
from transformers import DepthAnythingConfig, Dinov2Config
|
||||
from transformers.file_utils import is_torch_available, is_vision_available
|
||||
from transformers.pytorch_utils import is_torch_greater_or_equal_than_2_4
|
||||
from transformers.testing_utils import require_torch, require_vision, slow, torch_device
|
||||
|
||||
from ...test_configuration_common import ConfigTester
|
||||
@@ -290,3 +291,30 @@ class DepthAnythingModelIntegrationTest(unittest.TestCase):
|
||||
).to(torch_device)
|
||||
|
||||
self.assertTrue(torch.allclose(predicted_depth[0, :3, :3], expected_slice, atol=1e-4))
|
||||
|
||||
def test_export(self):
|
||||
for strict in [True, False]:
|
||||
with self.subTest(strict=strict):
|
||||
if not is_torch_greater_or_equal_than_2_4:
|
||||
self.skipTest(reason="This test requires torch >= 2.4 to run.")
|
||||
model = (
|
||||
DepthAnythingForDepthEstimation.from_pretrained("LiheYoung/depth-anything-small-hf")
|
||||
.to(torch_device)
|
||||
.eval()
|
||||
)
|
||||
image_processor = DPTImageProcessor.from_pretrained("LiheYoung/depth-anything-small-hf")
|
||||
image = prepare_img()
|
||||
inputs = image_processor(images=image, return_tensors="pt").to(torch_device)
|
||||
|
||||
exported_program = torch.export.export(
|
||||
model,
|
||||
args=(inputs["pixel_values"],),
|
||||
strict=strict,
|
||||
)
|
||||
with torch.no_grad():
|
||||
eager_outputs = model(**inputs)
|
||||
exported_outputs = exported_program.module().forward(inputs["pixel_values"])
|
||||
self.assertEqual(eager_outputs.predicted_depth.shape, exported_outputs.predicted_depth.shape)
|
||||
self.assertTrue(
|
||||
torch.allclose(eager_outputs.predicted_depth, exported_outputs.predicted_depth, atol=1e-4)
|
||||
)
|
||||
|
||||
@@ -18,6 +18,7 @@ import unittest
|
||||
|
||||
from transformers import DPTConfig
|
||||
from transformers.file_utils import is_torch_available, is_vision_available
|
||||
from transformers.pytorch_utils import is_torch_greater_or_equal_than_2_4
|
||||
from transformers.testing_utils import require_torch, require_vision, slow, torch_device
|
||||
|
||||
from ...test_configuration_common import ConfigTester
|
||||
@@ -410,3 +411,24 @@ class DPTModelIntegrationTest(unittest.TestCase):
|
||||
).squeeze()
|
||||
self.assertTrue(output_enlarged.shape == expected_shape)
|
||||
self.assertTrue(torch.allclose(predicted_depth_l, output_enlarged, rtol=1e-3))
|
||||
|
||||
def test_export(self):
|
||||
for strict in [True, False]:
|
||||
with self.subTest(strict=strict):
|
||||
if not is_torch_greater_or_equal_than_2_4:
|
||||
self.skipTest(reason="This test requires torch >= 2.4 to run.")
|
||||
model = DPTForSemanticSegmentation.from_pretrained("Intel/dpt-large-ade").to(torch_device).eval()
|
||||
image_processor = DPTImageProcessor.from_pretrained("Intel/dpt-large-ade")
|
||||
image = prepare_img()
|
||||
inputs = image_processor(images=image, return_tensors="pt").to(torch_device)
|
||||
|
||||
exported_program = torch.export.export(
|
||||
model,
|
||||
args=(inputs["pixel_values"],),
|
||||
strict=strict,
|
||||
)
|
||||
with torch.no_grad():
|
||||
eager_outputs = model(**inputs)
|
||||
exported_outputs = exported_program.module().forward(inputs["pixel_values"])
|
||||
self.assertEqual(eager_outputs.logits.shape, exported_outputs.logits.shape)
|
||||
self.assertTrue(torch.allclose(eager_outputs.logits, exported_outputs.logits, atol=1e-4))
|
||||
|
||||
Reference in New Issue
Block a user