| 142 | } |
| 143 | |
| 144 | TensorValue wrap_tensor(ggml_tensor * tensor, const TensorShape & shape, ggml_type type) { |
| 145 | if (tensor == nullptr) { |
| 146 | throw std::runtime_error("Cannot wrap null ggml tensor"); |
| 147 | } |
| 148 | |
| 149 | ensure_positive_dims(shape); |
| 150 | const auto ggml_dims = to_ggml_dims(shape); |
| 151 | for (size_t i = 0; i < shape.rank; ++i) { |
| 152 | if (tensor->ne[i] != ggml_dims[i]) { |
| 153 | throw std::runtime_error("Wrapped ggml tensor shape does not match logical shape " + shape.to_string()); |
| 154 | } |
| 155 | } |
| 156 | |
| 157 | return TensorValue{tensor, shape, type}; |
| 158 | } |
| 159 | |
| 160 | TensorValue reshape_tensor(ModuleBuildContext & ctx, const TensorValue & value, const TensorShape & new_shape) { |
| 161 | if (!value.valid()) { |