Arguments.cpp 5.2 KB
Newer Older
1
/* Copyright (c) 2016 PaddlePaddle Authors. All Rights Reserved.
Z
zhangjinchao01 已提交
2 3 4 5 6 7 8 9 10 11 12 13 14 15

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License. */

#include "PaddleAPI.h"
E
emailweixu 已提交
16
#include "PaddleAPIPrivate.h"
Z
zhangjinchao01 已提交
17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40

#include "paddle/parameter/Argument.h"

size_t Arguments::getSlotNum() const { return m->outputs.size(); }

Arguments* Arguments::createArguments(size_t slotNum) {
  auto args = new Arguments();
  args->m->outputs.resize(slotNum);
  return args;
}

void Arguments::resize(size_t slotNum) { m->outputs.resize(slotNum); }

Arguments::Arguments() : m(new ArgumentsPrivate()) {}

Arguments::~Arguments() { delete m; }

Arguments* Arguments::createByPaddleArgumentVector(void* ptr) {
  auto p = (std::vector<paddle::Argument>*)(ptr);
  auto args = new Arguments();
  args->m->outputs = *p;
  return args;
}

L
liaogang 已提交
41 42 43 44 45 46 47
Arguments* Arguments::createByPaddleArgument(const void* ptr) {
  auto p = (paddle::Argument*)(ptr);
  auto args = new Arguments();
  args->m->outputs.push_back(*p);
  return args;
}

X
xuwei06 已提交
48 49 50 51 52 53 54 55 56 57
Matrix* Arguments::getSlotValue(size_t idx) const throw(RangeError) {
  auto& a = m->getArg(idx);
  return Matrix::createByPaddleMatrixPtr(&a.value);
}

Matrix* Arguments::getSlotGrad(size_t idx) const throw(RangeError) {
  auto& a = m->getArg(idx);
  return Matrix::createByPaddleMatrixPtr(&a.grad);
}

Z
zhangjinchao01 已提交
58 59 60 61 62 63 64 65 66 67 68 69 70 71 72
IVector* Arguments::getSlotIds(size_t idx) const throw(RangeError) {
  auto& a = m->getArg(idx);
  return IVector::createByPaddleVectorPtr(&a.ids);
}

Matrix* Arguments::getSlotIn(size_t idx) const throw(RangeError) {
  auto& a = m->getArg(idx);
  return Matrix::createByPaddleMatrixPtr(&a.in);
}

void Arguments::setSlotValue(size_t idx, Matrix* mat) throw(RangeError) {
  auto& a = m->getArg(idx);
  a.value = m->cast<paddle::Matrix>(mat->getSharedPtr());
}

X
xuwei06 已提交
73 74 75 76 77
void Arguments::setSlotGrad(size_t idx, Matrix* mat) throw(RangeError) {
  auto& a = m->getArg(idx);
  a.grad = m->cast<paddle::Matrix>(mat->getSharedPtr());
}

Z
zhangjinchao01 已提交
78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103
void Arguments::setSlotIn(size_t idx, Matrix* mat) throw(RangeError) {
  auto& a = m->getArg(idx);
  a.in = m->cast<paddle::Matrix>(mat->getSharedPtr());
}

void Arguments::setSlotIds(size_t idx, IVector* vec) throw(RangeError) {
  auto& a = m->getArg(idx);
  auto& v = m->cast<paddle::IVector>(vec->getSharedPtr());
  a.ids = v;
}

template <typename T1>
static inline void doCopyFromSafely(std::shared_ptr<T1>& dest,
                                    std::shared_ptr<T1>& src) {
  if (src) {
    if (dest) {
      dest->copyFrom(*src);
    } else {
      dest = src;
    }
  }
}

IVector* Arguments::getSlotSequenceStartPositions(size_t idx) const
    throw(RangeError) {
  auto& a = m->getArg(idx);
104 105 106 107 108 109 110 111
  if (a.sequenceStartPositions) {
    return IVector::createByPaddleVectorPtr(
        &a.sequenceStartPositions->getMutableVector(false));
  } else {
    return nullptr;
  }
}

Y
yuyang18 已提交
112 113
IVector* Arguments::getSlotSubSequenceStartPositions(size_t idx) const
    throw(RangeError) {
114 115 116 117 118 119 120
  auto& a = m->getArg(idx);
  if (a.subSequenceStartPositions) {
    return IVector::createByPaddleVectorPtr(
        &a.subSequenceStartPositions->getMutableVector(false));
  } else {
    return nullptr;
  }
Z
zhangjinchao01 已提交
121 122 123 124 125 126 127 128 129
}

void Arguments::setSlotSequenceStartPositions(size_t idx,
                                              IVector* vec) throw(RangeError) {
  auto& a = m->getArg(idx);
  auto& v = m->cast<paddle::IVector>(vec->getSharedPtr());
  a.sequenceStartPositions = std::make_shared<paddle::ICpuGpuVector>(v);
}

130
void Arguments::setSlotSubSequenceStartPositions(
131
    size_t idx, IVector* vec) throw(RangeError) {
132 133 134 135 136
  auto& a = m->getArg(idx);
  auto& v = m->cast<paddle::IVector>(vec->getSharedPtr());
  a.subSequenceStartPositions = std::make_shared<paddle::ICpuGpuVector>(v);
}

Z
zhangjinchao01 已提交
137 138 139 140 141 142 143 144 145 146
IVector* Arguments::getSlotSequenceDim(size_t idx) const throw(RangeError) {
  auto& a = m->getArg(idx);
  return IVector::createByPaddleVectorPtr(&a.cpuSequenceDims);
}

void Arguments::setSlotSequenceDim(size_t idx, IVector* vec) throw(RangeError) {
  auto& a = m->getArg(idx);
  a.cpuSequenceDims = m->cast<paddle::IVector>(vec->getSharedPtr());
}

147
float Arguments::sum() const { return paddle::Argument::sum(m->outputs); }
Y
Yu Yang 已提交
148

Z
zhangjinchao01 已提交
149 150 151 152 153
int64_t Arguments::getBatchSize(size_t idx) const throw(RangeError) {
  auto& a = m->getArg(idx);
  return a.getBatchSize();
}

154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173
void Arguments::setSlotFrameHeight(size_t idx, size_t h) throw(RangeError) {
  auto& a = m->getArg(idx);
  a.setFrameHeight(h);
}

void Arguments::setSlotFrameWidth(size_t idx, size_t w) throw(RangeError) {
  auto& a = m->getArg(idx);
  a.setFrameWidth(w);
}

size_t Arguments::getSlotFrameHeight(size_t idx) const throw(RangeError) {
  auto& a = m->getArg(idx);
  return a.getFrameHeight();
}

size_t Arguments::getSlotFrameWidth(size_t idx) const throw(RangeError) {
  auto& a = m->getArg(idx);
  return a.getFrameWidth();
}

Z
zhangjinchao01 已提交
174
void* Arguments::getInternalArgumentsPtr() const { return &m->outputs; }