| 10 | |
| 11 | |
| 12 | def test_layer_spec_validate(): |
| 13 | class SubSpec(ctranslate2.specs.LayerSpec): |
| 14 | def __init__(self): |
| 15 | self.a = np.ones([5], dtype=np.float16) |
| 16 | |
| 17 | class Spec(ctranslate2.specs.LayerSpec): |
| 18 | def __init__(self): |
| 19 | self.a = np.zeros([5], dtype=np.float32) |
| 20 | self.b = np.zeros([5], dtype=np.float16) |
| 21 | self.c = np.zeros([5], dtype=np.int32) |
| 22 | self.d = OPTIONAL |
| 23 | self.e = SubSpec() |
| 24 | self.f = True |
| 25 | self.g = "hello" |
| 26 | |
| 27 | spec = Spec() |
| 28 | spec.validate() |
| 29 | assert spec.a.dtype == "float32" |
| 30 | assert spec.b.dtype == "float16" |
| 31 | assert spec.c.dtype == "int32" |
| 32 | assert spec.d == OPTIONAL |
| 33 | assert spec.e.a.dtype == "float16" |
| 34 | assert test_utils.array_equal(spec.f.numpy(), np.int8(1)) |
| 35 | assert test_utils.array_equal( |
| 36 | spec.g.numpy(), np.array([104, 101, 108, 108, 111], dtype=np.int8) |
| 37 | ) |
| 38 | |
| 39 | with pytest.raises(AttributeError, match="Attribute z does not exist"): |
| 40 | spec.z = True |
| 41 | |
| 42 | |
| 43 | def test_layer_spec_validate_unset(): |