| 302 | }; |
| 303 | |
| 304 | class TensorMap { |
| 305 | private: |
| 306 | std::unordered_map<std::string, Tensor> tensor_map_; |
| 307 | |
| 308 | inline bool isValid(const Tensor& tensor) |
| 309 | { |
| 310 | return tensor.size() > 0 && tensor.data != nullptr; |
| 311 | } |
| 312 | |
| 313 | public: |
| 314 | TensorMap() = default; |
| 315 | TensorMap(const std::unordered_map<std::string, Tensor>& tensor_map); |
| 316 | TensorMap(const std::vector<Tensor>& tensor_map); |
| 317 | TensorMap(std::initializer_list<std::pair<std::string, Tensor>> tensor_map); |
| 318 | ~TensorMap(); |
| 319 | |
| 320 | inline size_t size() const |
| 321 | { |
| 322 | return tensor_map_.size(); |
| 323 | } |
| 324 | |
| 325 | inline bool isExist(const std::string& key) const |
| 326 | { |
| 327 | FT_LOG_DEBUG("%s for key: %s", __PRETTY_FUNCTION__, key.c_str()); |
| 328 | return tensor_map_.find(key) != tensor_map_.end(); |
| 329 | } |
| 330 | |
| 331 | std::vector<std::string> keys() const; |
| 332 | |
| 333 | inline void insert(const std::string& key, const Tensor& value) |
| 334 | { |
| 335 | FT_CHECK_WITH_INFO(!isExist(key), fmtstr("Duplicated key %s", key.c_str())); |
| 336 | FT_CHECK_WITH_INFO(isValid(value), fmtstr("A none tensor or nullptr is not allowed (key is %s)", key.c_str())); |
| 337 | tensor_map_.insert({key, value}); |
| 338 | } |
| 339 | |
| 340 | inline void insertIfValid(const std::string& key, const Tensor& value) |
| 341 | { |
| 342 | if (isValid(value)) { |
| 343 | insert({key, value}); |
| 344 | } |
| 345 | } |
| 346 | |
| 347 | inline void insert(std::pair<std::string, Tensor> p) |
| 348 | { |
| 349 | tensor_map_.insert(p); |
| 350 | } |
| 351 | |
| 352 | // prevent converting int or size_t to string automatically |
| 353 | Tensor at(int tmp) = delete; |
| 354 | Tensor at(size_t tmp) = delete; |
| 355 | |
| 356 | inline Tensor& at(const std::string& key) |
| 357 | { |
| 358 | FT_LOG_DEBUG("%s for key %s", __PRETTY_FUNCTION__, key.c_str()); |
| 359 | FT_CHECK_WITH_INFO(isExist(key), |
| 360 | fmtstr("Cannot find a tensor of name %s in the tensor map (keys: %s)", |
| 361 | key.c_str(), |
no test coverage detected