|
| 1 | +import tempfile |
| 2 | +from pathlib import Path |
| 3 | + |
| 4 | +import halide as hl |
| 5 | +import numpy as np |
| 6 | + |
| 7 | + |
| 8 | +def test_serialize_deserialize_pipeline_file(): |
| 9 | + """Test serializing and deserializing a pipeline to/from a file.""" |
| 10 | + x, y = hl.vars("x y") |
| 11 | + f = hl.Func("f") |
| 12 | + f[x, y] = x + y |
| 13 | + |
| 14 | + pipeline = hl.Pipeline(f) |
| 15 | + |
| 16 | + with tempfile.NamedTemporaryFile(suffix=".hlpipe", delete=False) as tmp: |
| 17 | + filename = Path(tmp.name) |
| 18 | + |
| 19 | + try: |
| 20 | + hl.serialize_pipeline(pipeline, filename) |
| 21 | + |
| 22 | + assert filename.exists() |
| 23 | + assert filename.stat().st_size > 0 |
| 24 | + |
| 25 | + deserialized_pipeline = hl.deserialize_pipeline(filename) |
| 26 | + |
| 27 | + result = deserialized_pipeline.realize([10, 10]) |
| 28 | + assert result.dim(0).extent() == 10 |
| 29 | + assert result.dim(1).extent() == 10 |
| 30 | + |
| 31 | + expected = np.add.outer(np.arange(10), np.arange(10)) |
| 32 | + assert np.array_equal(np.array(result), expected) |
| 33 | + |
| 34 | + finally: |
| 35 | + filename.unlink(missing_ok=True) |
| 36 | + |
| 37 | + |
| 38 | +def test_serialize_deserialize_pipeline_bytes(): |
| 39 | + """Test serializing and deserializing a pipeline to/from bytes.""" |
| 40 | + x, y = hl.Var("x"), hl.Var("y") |
| 41 | + f = hl.Func("f") |
| 42 | + f[x, y] = x * 2 + y |
| 43 | + |
| 44 | + pipeline = hl.Pipeline(f) |
| 45 | + |
| 46 | + data = hl.serialize_pipeline(pipeline) |
| 47 | + assert isinstance(data, bytes) |
| 48 | + assert len(data) > 0 |
| 49 | + |
| 50 | + deserialized_pipeline = hl.deserialize_pipeline(data) |
| 51 | + |
| 52 | + result = deserialized_pipeline.realize([5, 5]) |
| 53 | + expected = np.fromfunction(lambda x, y: x * 2 + y, (5, 5), dtype=int).transpose() |
| 54 | + assert np.array_equal(np.array(result), expected) |
| 55 | + |
| 56 | + |
| 57 | +def test_serialize_deserialize_with_parameters(): |
| 58 | + """Test serializing and deserializing a pipeline with external parameters.""" |
| 59 | + x = hl.Var("x") |
| 60 | + p = hl.Param(hl.Int(32), "multiplier", 1) |
| 61 | + f = hl.Func("f") |
| 62 | + f[x] = x * p |
| 63 | + |
| 64 | + pipeline = hl.Pipeline(f) |
| 65 | + |
| 66 | + with tempfile.NamedTemporaryFile(suffix=".hlpipe", delete=False) as tmp: |
| 67 | + filename = Path(tmp.name) |
| 68 | + |
| 69 | + try: |
| 70 | + params = hl.serialize_pipeline(pipeline, filename, get_params=True) |
| 71 | + |
| 72 | + assert "multiplier" in params |
| 73 | + assert params["multiplier"].name() == "multiplier" |
| 74 | + |
| 75 | + user_params = {"multiplier": hl.Param(hl.Int(32), "multiplier", 5).parameter()} |
| 76 | + deserialized_pipeline = hl.deserialize_pipeline(filename, user_params) |
| 77 | + |
| 78 | + result = deserialized_pipeline.realize([3]) |
| 79 | + assert list(result) == [0, 5, 10] |
| 80 | + |
| 81 | + finally: |
| 82 | + filename.unlink(missing_ok=True) |
| 83 | + |
| 84 | + |
| 85 | +def test_serialize_deserialize_with_parameters_bytes(): |
| 86 | + """Test serializing and deserializing a pipeline with parameters using bytes.""" |
| 87 | + x = hl.Var("x") |
| 88 | + p = hl.Param(hl.Int(32), "offset", 0) |
| 89 | + f = hl.Func("f") |
| 90 | + f[x] = x + p |
| 91 | + |
| 92 | + pipeline = hl.Pipeline(f) |
| 93 | + |
| 94 | + data, params = hl.serialize_pipeline(pipeline, get_params=True) |
| 95 | + |
| 96 | + assert "offset" in params |
| 97 | + assert params["offset"].name() == "offset" |
| 98 | + |
| 99 | + user_params = {"offset": hl.Param(hl.Int(32), "offset", 100).parameter()} |
| 100 | + deserialized_pipeline = hl.deserialize_pipeline(data, user_params) |
| 101 | + |
| 102 | + result = deserialized_pipeline.realize([3]) |
| 103 | + assert list(result) == [100, 101, 102] |
| 104 | + |
| 105 | + |
| 106 | +def test_deserialize_parameters_file(): |
| 107 | + """Test deserializing just the parameters from a file.""" |
| 108 | + x = hl.Var("x") |
| 109 | + p1 = hl.Param(hl.Int(32), "param1", 1) |
| 110 | + p2 = hl.Param(hl.Float(32), "param2", 2.0) |
| 111 | + f = hl.Func("f") |
| 112 | + f[x] = hl.cast(hl.Int(32), x * p1 + p2) |
| 113 | + |
| 114 | + pipeline = hl.Pipeline(f) |
| 115 | + |
| 116 | + with tempfile.NamedTemporaryFile(suffix=".hlpipe", delete=False) as tmp: |
| 117 | + filename = Path(tmp.name) |
| 118 | + |
| 119 | + try: |
| 120 | + hl.serialize_pipeline(pipeline, filename) |
| 121 | + params = hl.deserialize_parameters(filename) |
| 122 | + |
| 123 | + assert "param1" in params |
| 124 | + assert "param2" in params |
| 125 | + assert params["param1"].name() == "param1" |
| 126 | + assert params["param2"].name() == "param2" |
| 127 | + assert params["param1"].type() == hl.Int(32) |
| 128 | + assert params["param2"].type() == hl.Float(32) |
| 129 | + |
| 130 | + finally: |
| 131 | + filename.unlink(missing_ok=True) |
| 132 | + |
| 133 | + |
| 134 | +def test_deserialize_parameters_bytes(): |
| 135 | + """Test deserializing just the parameters from bytes.""" |
| 136 | + x = hl.Var("x") |
| 137 | + p1 = hl.Param(hl.UInt(16), "width", 64) |
| 138 | + p2 = hl.Param(hl.UInt(16), "height", 64) |
| 139 | + f = hl.Func("f") |
| 140 | + f[x] = hl.select(x < p1, p2, 0) |
| 141 | + |
| 142 | + pipeline = hl.Pipeline(f) |
| 143 | + |
| 144 | + data = hl.serialize_pipeline(pipeline) |
| 145 | + params = hl.deserialize_parameters(data) |
| 146 | + |
| 147 | + assert "width" in params |
| 148 | + assert "height" in params |
| 149 | + assert params["width"].type() == hl.UInt(16) |
| 150 | + assert params["height"].type() == hl.UInt(16) |
| 151 | + |
| 152 | + |
| 153 | +def test_pipeline_with_multiple_outputs(): |
| 154 | + """Test serializing/deserializing a pipeline with multiple outputs.""" |
| 155 | + x, y = hl.vars("x y") |
| 156 | + |
| 157 | + f1 = hl.Func("f1") |
| 158 | + f1[x, y] = x + y |
| 159 | + |
| 160 | + f2 = hl.Func("f2") |
| 161 | + f2[x, y] = x - y |
| 162 | + |
| 163 | + pipeline = hl.Pipeline([f1, f2]) |
| 164 | + |
| 165 | + data = hl.serialize_pipeline(pipeline) |
| 166 | + deserialized_pipeline = hl.deserialize_pipeline(data) |
| 167 | + |
| 168 | + results = deserialized_pipeline.realize([5, 5]) |
| 169 | + assert len(results) == 2 |
| 170 | + |
| 171 | + expected = np.add.outer(np.arange(5), np.arange(5)) |
| 172 | + assert np.array_equal(np.array(results[0]), expected) |
| 173 | + |
| 174 | + expected = np.subtract.outer(np.arange(5), np.arange(5)).transpose() |
| 175 | + assert np.array_equal(np.array(results[1]), expected) |
| 176 | + |
| 177 | + |
| 178 | +def test_empty_user_params(): |
| 179 | + """Test that empty user_params works correctly.""" |
| 180 | + x = hl.Var("x") |
| 181 | + f = hl.Func("f") |
| 182 | + f[x] = x * x |
| 183 | + |
| 184 | + pipeline = hl.Pipeline(f) |
| 185 | + data = hl.serialize_pipeline(pipeline) |
| 186 | + |
| 187 | + deserialized1 = hl.deserialize_pipeline(data, {}) |
| 188 | + result1 = deserialized1.realize([3]) |
| 189 | + assert list(result1) == [0, 1, 4] |
| 190 | + |
| 191 | + deserialized2 = hl.deserialize_pipeline(data) |
| 192 | + result2 = deserialized2.realize([3]) |
| 193 | + assert list(result2) == [0, 1, 4] |
| 194 | + |
| 195 | + |
| 196 | +if __name__ == "__main__": |
| 197 | + test_serialize_deserialize_pipeline_file() |
| 198 | + test_serialize_deserialize_pipeline_bytes() |
| 199 | + test_serialize_deserialize_with_parameters() |
| 200 | + test_serialize_deserialize_with_parameters_bytes() |
| 201 | + test_deserialize_parameters_file() |
| 202 | + test_deserialize_parameters_bytes() |
| 203 | + test_pipeline_with_multiple_outputs() |
| 204 | + test_empty_user_params() |
| 205 | + print("Success!") |
0 commit comments