Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions python/tests/cuda_skip.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,4 +74,5 @@
"TestQuantized.test_small_matrix",
"TestQuantized.test_throw",
"TestQuantized.test_vjp_scales_biases",
"TestExportImport.test_export_quantized_model",
}
40 changes: 40 additions & 0 deletions python/tests/test_export_import.py
Original file line number Diff line number Diff line change
Expand Up @@ -346,6 +346,46 @@ def forward(x):
expected = forward(input_data)
self.assertTrue(mx.allclose(expected, out))

def test_export_control_flow(self):

def fun(x, y):
if y.shape[0] <= 2:
return x + y
else:
return x + 2 * y

for y in (mx.array([1, 2, 3]), mx.array([1, 2])):
for shapeless in (True, False):
with self.subTest(y=y, shapeless=shapeless):
x = mx.array(1)
export_path = os.path.join(self.test_dir, "control_flow.mlxfn")
mx.export_function(export_path, fun, x, y, shapeless=shapeless)

imported_fn = mx.import_function(export_path)
self.assertTrue(mx.array_equal(imported_fn(x, y)[0], fun(x, y)))

def test_export_quantized_model(self):
for shapeless in (True, False):
with self.subTest(shapeless=shapeless):
model = nn.Sequential(
nn.Linear(1024, 512), nn.ReLU(), nn.Linear(512, 1024)
)
model.eval()
mx.eval(model.parameters())
input_data = mx.ones(shape=(512, 1024))
nn.quantize(model)
self.assertTrue(isinstance(model.layers[0], nn.QuantizedLinear))
self.assertTrue(isinstance(model.layers[2], nn.QuantizedLinear))
mx.eval(model.parameters())

export_path = os.path.join(self.test_dir, "quantized_linear.mlxfn")
mx.export_function(export_path, model, input_data, shapeless=shapeless)

imported_fn = mx.import_function(export_path)
self.assertTrue(
mx.array_equal(imported_fn(input_data)[0], model(input_data))
)


if __name__ == "__main__":
mlx_tests.MLXTestRunner()