You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

storage.cc 1.6 kB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. /**
  2. * Copyright 2019 Huawei Technologies Co., Ltd
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #include "common/storage.h"
  17. #include "flatbuffers/flatbuffers.h"
  18. #include "common/mslog.h"
  19. #include "common/file_utils.h"
  20. namespace mindspore {
  21. namespace predict {
  22. int Storage::Save(const GraphDefT &graph, const std::string &outputPath) {
  23. flatbuffers::FlatBufferBuilder builder(flatSize);
  24. auto offset = GraphDef::Pack(builder, &graph);
  25. builder.Finish(offset);
  26. int size = builder.GetSize();
  27. auto content = builder.GetBufferPointer();
  28. if (content == nullptr) {
  29. MS_LOGE("GetBufferPointer nullptr");
  30. return RET_ERROR;
  31. }
  32. std::string realPath = RealPath(outputPath.c_str());
  33. if (realPath.empty()) {
  34. MS_LOGE("Output file path '%s' is not valid", outputPath.c_str());
  35. return RET_ERROR;
  36. }
  37. std::ofstream output(realPath, std::ofstream::binary);
  38. if (!output.is_open()) {
  39. MS_LOGE("ofstream open failed");
  40. return RET_ERROR;
  41. }
  42. output.write((const char *)content, size);
  43. output.close();
  44. return RET_OK;
  45. }
  46. } // namespace predict
  47. } // namespace mindspore