full.cc 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. // Copyright (c) 2022 PaddlePaddle Authors. All Rights Reserved.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. #include "ultra_infer/function/full.h"
  15. #include "ultra_infer/function/eigen.h"
  16. #include <algorithm>
  17. namespace ultra_infer {
  18. namespace function {
  19. template <typename T> void FullValue(FDTensor *tensor, const Scalar &val) {
  20. auto t = EigenVector<T>::Flatten(*tensor);
  21. auto &place = *EigenDeviceWrapper::GetInstance()->GetDevice();
  22. t.device(place) = t.constant(val.to<T>());
  23. }
  24. void Full(const Scalar &value, const std::vector<int64_t> &shape, FDTensor *out,
  25. FDDataType dtype) {
  26. FD_VISIT_ALL_TYPES(dtype, "Full", ([&] {
  27. out->Allocate(shape, dtype);
  28. FullValue<data_t>(out, value);
  29. }));
  30. }
  31. void FullLike(const FDTensor &x, const Scalar &value, FDTensor *out,
  32. FDDataType dtype) {
  33. Full(value, x.Shape(), out, dtype);
  34. }
  35. } // namespace function
  36. } // namespace ultra_infer