content: /*
   * Tencent is pleased to support the open source community by making Angel available.
   *
   * Copyright (C) 2017-2018 THL A29 Limited, a Tencent company. 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
   *
   * https://opensource.org/licenses/Apache-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.
   *
   */
  
  
  package com.tencent.angel.ml.dnn;
  
  import com.tencent.angel.conf.AngelConf;
  import com.tencent.angel.ml.core.conf.MLConf;
  import com.tencent.angel.ml.core.graphsubmit.GraphRunner;
  import org.apache.commons.logging.Log;
  import org.apache.commons.logging.LogFactory;
  import org.apache.hadoop.conf.Configuration;
  import org.apache.hadoop.fs.FileSystem;
  import org.apache.hadoop.mapreduce.lib.input.CombineTextInputFormat;
  import org.apache.log4j.PropertyConfigurator;
  import org.junit.Test;
  
  /**
   * Gradient descent LR UT.
   */
  public class DNNTest {
    private Configuration conf = new Configuration();
    private static final Log LOG = LogFactory.getLog(DNNTest.class);
    private static String LOCAL_FS = FileSystem.DEFAULT_FS;
    private static String CLASSBASE = "com.tencent.angel.ml.core.graphsubmit.";
    private static String TMP_PATH = System.getProperty("java.io.tmpdir", "/tmp");
  
    static {
      PropertyConfigurator.configure("../conf/log4j.properties");
    }
  
    private void setSystemConf() throws Exception {
      try {
        // Set local deploy mode
        conf.set(AngelConf.ANGEL_DEPLOY_MODE, "LOCAL");
  
        // Set basic configuration keys
        conf.setBoolean("mapred.mapper.new-api", true);
        conf.set(AngelConf.ANGEL_INPUTFORMAT_CLASS, CombineTextInputFormat.class.getName());
        conf.setBoolean(AngelConf.ANGEL_JOB_OUTPUT_PATH_DELETEONEXIST, true);
        conf.setInt(AngelConf.ANGEL_PSAGENT_CACHE_SYNC_TIMEINTERVAL_MS, 10);
        conf.setInt(AngelConf.ANGEL_WORKER_HEARTBEAT_INTERVAL_MS, 1000);
        conf.setInt(AngelConf.ANGEL_PS_HEARTBEAT_INTERVAL_MS, 1000);
  
        //set angel resource parameters #worker, #task, #PS
        conf.setInt(AngelConf.ANGEL_WORKERGROUP_NUMBER, 1);
        conf.setInt(AngelConf.ANGEL_WORKER_TASK_NUMBER, 1);
        conf.setInt(AngelConf.ANGEL_PS_NUMBER, 1);
  
        String savePath = LOCAL_FS + TMP_PATH + "/model/DNN";
        String logPath = LOCAL_FS + TMP_PATH + "/tempLog";
        // Set save model path
        conf.set(AngelConf.ANGEL_SAVE_MODEL_PATH, savePath);
        // Set log path
        conf.set(AngelConf.ANGEL_LOG_PATH, logPath);
  
        String angelConfFile = "./src/test/jsons/dnn.json";
        conf.set(AngelConf.ANGEL_ML_CONF, angelConfFile);
        conf.set(MLConf.ML_MODEL_CLASS_NAME(), CLASSBASE + "GraphModel");
  
      } catch (Exception x) {
        LOG.error("setup failed ", x);
        throw x;
      }
    }
  
  
    @Test public void testDNN() throws Exception {
      setSystemConf();
      trainTest();
      predictTest();
    }
  
    private void trainTest() throws Exception {
      try {
        String inputPath = "../../data/w6a/w6a_300d_train.libsvm";
        conf.set(AngelConf.ANGEL_TRAIN_DATA_PATH, inputPath);
  
        // Set actionType train
        conf.set(AngelConf.ANGEL_ACTION_TYPE, MLConf.ANGEL_ML_TRAIN());
  
        GraphRunner runner = new GraphRunner();
        runner.train(conf);
      } catch (Exception x) {
        LOG.error("run trainOnLocalClusterTest failed ", x);
        throw x;
      }
    }
  
    private void predictTest() throws Exception {
      try {
        String inputPath = "../../data/w6a/w6a_300d_test.libsvm";
        String loadPath = LOCAL_FS + TMP_PATH + "/model/DNN";
        String predictPath = LOCAL_FS + TMP_PATH + "/predict";
  
        // Set trainning data path
        conf.set(AngelConf.ANGEL_PREDICT_DATA_PATH, inputPath);
  
        conf.set(AngelConf.ANGEL_LOAD_MODEL_PATH, loadPath);
  
        // Set predict result path
        conf.set(AngelConf.ANGEL_PREDICT_PATH, predictPath);
  
        // Set actionType prediction
        conf.set(AngelConf.ANGEL_ACTION_TYPE, MLConf.ANGEL_ML_PREDICT());
  
        GraphRunner runner = new GraphRunner();
        runner.predict(conf);
      } catch (Exception x) {
        LOG.error("run predictTest failed ", x);
        throw x;
      }
    }
  }
  