config.go 5.3 KB
Newer Older
F
flame 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
// Copyright (c) 2019 PaddlePaddle 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.

package paddle

// #cgo CFLAGS: -Ipaddle_c/paddle/include
// #cgo LDFLAGS: -Lpaddle_c/paddle/lib -lpaddle_fluid_c
// #include <stdbool.h>
// #include <stdlib.h>
// #include <paddle_c_api.h>
import "C"

import "runtime"
import "unsafe"

27 28 29 30 31 32 33 34
type Precision C.Precision

const (
	Precision_FLOAT32 Precision = C.kFloat32
	Precision_INT8    Precision = C.kInt8
	Precision_HALF    Precision = C.kHalf
)

F
flame 已提交
35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53
type AnalysisConfig struct {
	c *C.PD_AnalysisConfig
}

func NewAnalysisConfig() *AnalysisConfig {
	c_config := C.PD_NewAnalysisConfig()
	config := &AnalysisConfig{c: c_config}
	runtime.SetFinalizer(config, (*AnalysisConfig).finalize)
	return config
}

func (config *AnalysisConfig) finalize() {
	C.PD_DeleteAnalysisConfig(config.c)
}

func (config *AnalysisConfig) SetModel(model, params string) {
	//C.printString((*C.char)(unsafe.Pointer(&s[0])))
	c_model := C.CString(model)
	defer C.free(unsafe.Pointer(c_model))
F
flame 已提交
54 55 56 57 58 59 60
	var c_params *C.char
	if params == "" {
		c_params = nil
	} else {
		c_params = C.CString(params)
		defer C.free(unsafe.Pointer(c_params))
	}
F
flame 已提交
61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76

	C.PD_SetModel(config.c, c_model, c_params)
}

func (config *AnalysisConfig) ModelDir() string {
	return C.GoString(C.PD_ModelDir(config.c))
}

func (config *AnalysisConfig) ProgFile() string {
	return C.GoString(C.PD_ProgFile(config.c))
}

func (config *AnalysisConfig) ParamsFile() string {
	return C.GoString(C.PD_ParamsFile(config.c))
}

F
flame 已提交
77 78
func (config *AnalysisConfig) EnableUseGpu(memory_pool_init_size_mb int, device_id int) {
	C.PD_EnableUseGpu(config.c, C.int(memory_pool_init_size_mb), C.int(device_id))
F
flame 已提交
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 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128
}

func (config *AnalysisConfig) DisableGpu() {
	C.PD_DisableGpu(config.c)
}

func (config *AnalysisConfig) UseGpu() bool {
	return ConvertCBooleanToGo(C.PD_UseGpu(config.c))
}

func (config *AnalysisConfig) GpuDeviceId() int {
	return int(C.PD_GpuDeviceId(config.c))
}

func (config *AnalysisConfig) MemoryPoolInitSizeMb() int {
	return int(C.PD_MemoryPoolInitSizeMb(config.c))
}

func (config *AnalysisConfig) EnableCudnn() {
	C.PD_EnableCUDNN(config.c)
}

func (config *AnalysisConfig) CudnnEnabled() bool {
	return ConvertCBooleanToGo(C.PD_CudnnEnabled(config.c))
}

func (config *AnalysisConfig) SwitchIrOptim(x bool) {
	C.PD_SwitchIrOptim(config.c, C.bool(x))
}

func (config *AnalysisConfig) IrOptim() bool {
	return ConvertCBooleanToGo(C.PD_IrOptim(config.c))
}

func (config *AnalysisConfig) SwitchUseFeedFetchOps(x bool) {
	C.PD_SwitchUseFeedFetchOps(config.c, C.bool(x))
}

func (config *AnalysisConfig) UseFeedFetchOpsEnabled() bool {
	return ConvertCBooleanToGo(C.PD_UseFeedFetchOpsEnabled(config.c))
}

func (config *AnalysisConfig) SwitchSpecifyInputNames(x bool) {
	C.PD_SwitchSpecifyInputNames(config.c, C.bool(x))
}

func (config *AnalysisConfig) SpecifyInputName() bool {
	return ConvertCBooleanToGo(C.PD_SpecifyInputName(config.c))
}

F
flame 已提交
129 130 131
func (config *AnalysisConfig) EnableTensorRtEngine(workspace_size int, max_batch_size int, min_subgraph_size int, precision Precision, use_static bool, use_calib_mode bool) {
	C.PD_EnableTensorRtEngine(config.c, C.int(workspace_size), C.int(max_batch_size), C.int(min_subgraph_size), C.Precision(precision), C.bool(use_static), C.bool(use_calib_mode))
}
F
flame 已提交
132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184

func (config *AnalysisConfig) TensorrtEngineEnabled() bool {
	return ConvertCBooleanToGo(C.PD_TensorrtEngineEnabled(config.c))
}

func (config *AnalysisConfig) SwitchIrDebug(x bool) {
	C.PD_SwitchIrDebug(config.c, C.bool(x))
}

func (config *AnalysisConfig) EnableMkldnn() {
	C.PD_EnableMKLDNN(config.c)
}

func (config *AnalysisConfig) SetCpuMathLibraryNumThreads(n int) {
	C.PD_SetCpuMathLibraryNumThreads(config.c, C.int(n))
}

func (config *AnalysisConfig) CpuMathLibraryNumThreads() int {
	return int(C.PD_CpuMathLibraryNumThreads(config.c))
}

func (config *AnalysisConfig) EnableMkldnnQuantizer() {
	C.PD_EnableMkldnnQuantizer(config.c)
}

func (config *AnalysisConfig) MkldnnQuantizerEnabled() bool {
	return ConvertCBooleanToGo(C.PD_MkldnnQuantizerEnabled(config.c))
}

// SetModelBuffer
// ModelFromMemory

func (config *AnalysisConfig) EnableMemoryOptim() {
	C.PD_EnableMemoryOptim(config.c)
}

func (config *AnalysisConfig) MemoryOptimEnabled() bool {
	return ConvertCBooleanToGo(C.PD_MemoryOptimEnabled(config.c))
}

func (config *AnalysisConfig) EnableProfile() {
	C.PD_EnableProfile(config.c)
}

func (config *AnalysisConfig) ProfileEnabled() bool {
	return ConvertCBooleanToGo(C.PD_ProfileEnabled(config.c))
}

func (config *AnalysisConfig) DisableGlogInfo() {
	C.PD_DisableGlogInfo(config.c)
}

func (config *AnalysisConfig) DeletePass(pass string) {
F
flame 已提交
185 186 187
	c_pass := C.CString(pass)
	defer C.free(unsafe.Pointer(c_pass))
	C.PD_DeletePass(config.c, c_pass)
F
flame 已提交
188 189 190
}

func (config *AnalysisConfig) SetInValid() {
F
flame 已提交
191
	C.PD_SetInValid(config.c)
F
flame 已提交
192 193 194
}

func (config *AnalysisConfig) IsValid() bool {
F
flame 已提交
195
	return ConvertCBooleanToGo(C.PD_IsValid(config.c))
F
flame 已提交
196
}