layer.py 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. # Copyright (c) 2019 PaddlePaddle Authors. All Rights Reserved.
  2. # Copyright (c) 2021 NVIDIA Corporation. All rights reserved.
  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. from paddle.base import core
  16. from paddle.base.core import Load
  17. class Layer:
  18. def __init__(self):
  19. self.cpp_layer = None
  20. # {name: Function}
  21. self.functions = {}
  22. def load(self, load_path, place):
  23. self.cpp_layer = Load(load_path, place)
  24. for name in self.cpp_layer.function_names():
  25. function = self.cpp_layer.function(name)
  26. info = self.cpp_layer.function_info(name)
  27. self.functions[name] = Function(function, info)
  28. setattr(self, name, self.functions[name])
  29. class Function:
  30. def __init__(self, function, info):
  31. self.function = function
  32. self.info = FunctionInfo(info)
  33. def __call__(self, *args):
  34. return core.eager.jit_function_call(self.function, args)
  35. class FunctionInfo:
  36. def __init__(self, info):
  37. self.info = info
  38. def name(self):
  39. return self.info.name()