job_desc.cpp 3.9 KB
Newer Older
S
Shenghang Tsai 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
/*
Copyright 2020 The OneFlow Authors. All rights reserved.

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.
*/
W
willzhang4a58 已提交
16
#include "oneflow/core/job/job_desc.h"
X
JobSet  
Xinqi 已提交
17
#include "oneflow/core/job/job_set.pb.h"
qq_22305325's avatar
qq_22305325 已提交
18
#include "oneflow/core/job/job_conf.cfg.h"
J
Jinhui Yuan 已提交
19
#include "oneflow/core/job/parallel_desc.h"
L
Li Xinqi 已提交
20
#include "oneflow/core/job/global_for.h"
L
Li Xinqi 已提交
21
#include "oneflow/core/operator/operator.h"
J
jiyuan 已提交
22
#include "oneflow/core/common/protobuf.h"
W
willzhang4a58 已提交
23
#include "oneflow/core/persistence/hadoop/hadoop_file_system.h"
L
Li Xinqi 已提交
24 25
#include "oneflow/core/graph/graph.h"
#include "oneflow/core/graph/op_graph.h"
N
Niu Chong 已提交
26
#include "oneflow/core/job/job_builder.h"
X
Xinqi 已提交
27
#include "oneflow/core/job/job_desc.h"
28
#include "oneflow/core/job/global_for.h"
W
willzhang4a58 已提交
29 30 31

namespace oneflow {

L
lixinqi 已提交
32 33
namespace {

L
lixinqi 已提交
34
void CheckFunctionConfig(const JobConfigProto& job_conf) {
L
Li Xinqi 已提交
35
  const auto& attr_name2attr_def = GlobalFunctionConfigDef().attr_name2attr_def();
L
lixinqi 已提交
36
  for (const auto& pair : job_conf.flag_name2flag_value()) {
L
Li Xinqi 已提交
37 38
    const auto& iter = attr_name2attr_def.find(pair.first);
    CHECK(iter != attr_name2attr_def.end());
L
lixinqi 已提交
39
    CHECK_EQ(iter->second.default_val().value_case(), pair.second.value_case());
L
lixinqi 已提交
40 41 42 43 44
  }
}

}  // namespace

L
Li Xinqi 已提交
45
JobDesc::JobDesc(const JobConfigProto& job_conf, int64_t job_id)
46
    : job_conf_(job_conf), job_id_(job_id), symbol_id_(NullOpt) {
qq_22305325's avatar
qq_22305325 已提交
47
  CHECK_JUST(Init());
qq_22305325's avatar
qq_22305325 已提交
48
  Global<ResourceDesc, ForSession>::Get()->DumpCudnnConf(job_conf);
L
Li Xinqi 已提交
49
}
L
Li Xinqi 已提交
50

qq_22305325's avatar
qq_22305325 已提交
51 52
Maybe<JobDesc> JobDesc::New(int64_t symbol_id, const JobConfigProto& job_conf) {
  auto job_desc = std::make_shared<JobDesc>(job_conf);
53
  job_desc->symbol_id_ = symbol_id;
qq_22305325's avatar
qq_22305325 已提交
54 55 56 57 58
  return job_desc;
}

Maybe<void> JobDesc::Init() {
  cfg_job_conf_.reset(new cfg::JobConfigProto(job_conf_));
59

W
willzhang4a58 已提交
60
#ifndef WITH_CUDA
qq_22305325's avatar
qq_22305325 已提交
61
  CHECK_EQ_OR_RETURN((Global<ResourceDesc, ForSession>::Get()->GpuDeviceNum()), 0);
W
willzhang4a58 已提交
62
#endif
L
lixinqi 已提交
63
  CheckFunctionConfig(job_conf_);
qq_22305325's avatar
qq_22305325 已提交
64
  return Maybe<void>::Ok();
L
lixinqi 已提交
65 66
}

67
const AttrValue& JobDesc::GetFunctionFlagVal(const std::string& field_name) const {
L
lixinqi 已提交
68 69
  const auto& iter = job_conf_.flag_name2flag_value().find(field_name);
  if (iter != job_conf_.flag_name2flag_value().end()) { return iter->second; }
L
Li Xinqi 已提交
70 71 72
  const auto& attr_name2attr_def = GlobalFunctionConfigDef().attr_name2attr_def();
  const auto& def_iter = attr_name2attr_def.find(field_name);
  CHECK(def_iter != attr_name2attr_def.end());
L
lixinqi 已提交
73
  return def_iter->second.default_val();
J
Jinhui Yuan 已提交
74 75
}

X
Xinqi 已提交
76
bool IsInterfaceOpConf(const OperatorConf& op_conf) {
77
  return IsClassRegistered<int32_t, IsInterfaceOpConf4OpTypeCase>(op_conf.op_type_case());
X
Xinqi 已提交
78 79
}

L
Li Xinqi 已提交
80 81
GlobalJobDescScope::GlobalJobDescScope(const JobConfigProto& job_conf, int64_t job_id) {
  Global<JobDesc>::New(job_conf, job_id);
X
Xinqi 已提交
82 83
}

L
Li Xinqi 已提交
84 85 86 87 88 89 90 91 92
GlobalJobDescScope::~GlobalJobDescScope() { Global<JobDesc>::Delete(); }

const JobDesc& GlobalJobDesc() { return *Global<JobDesc>::Get(); }

bool IsPullJob(const std::string& job_name, const InterUserJobInfo& inter_user_job_info) {
  for (const auto& pair : inter_user_job_info.output_or_var_op_name2pull_job_name()) {
    if (pair.second == job_name) { return true; }
  }
  return false;
X
Xinqi 已提交
93 94
}

L
Li Xinqi 已提交
95 96 97 98 99
bool IsPushJob(const std::string& job_name, const InterUserJobInfo& inter_user_job_info) {
  for (const auto& pair : inter_user_job_info.input_or_var_op_name2push_job_name()) {
    if (pair.second == job_name) { return true; }
  }
  if (job_name == inter_user_job_info.global_model_init_job_name()) { return true; }
J
Juncheng 已提交
100 101
  if (job_name == inter_user_job_info.global_model_load_job_name()) { return true; }
  if (job_name == inter_user_job_info.global_model_save_job_name()) { return true; }
L
Li Xinqi 已提交
102 103
  return false;
}
X
Xinqi 已提交
104

W
willzhang4a58 已提交
105
}  // namespace oneflow