config.go 4.7 KB
Newer Older
L
LKKlein 已提交
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 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 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 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 129 130 131 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
package paddle

// #cgo CFLAGS: -I../paddle_c/paddle/include
// #cgo LDFLAGS: -L${SRCDIR}/../paddle_c/paddle/lib -Wl,-rpath,$ORIGIN/paddle_c/paddle/lib -lpaddle_fluid_c
// #include <stdbool.h>
// #include <stdlib.h>
// #include <paddle_c_api.h>
import "C"

import (
	"runtime"
	"unsafe"
)

type Precision C.Precision

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

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_model := C.CString(model)
	defer C.free(unsafe.Pointer(c_model))
	var c_params *C.char
	if params == "" {
		c_params = nil
	} else {
		c_params = C.CString(params)
		defer C.free(unsafe.Pointer(c_params))
	}

	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))
}

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))
}

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))
}

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))
}

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) {
	c_pass := C.CString(pass)
	defer C.free(unsafe.Pointer(c_pass))
	C.PD_DeletePass(config.c, c_pass)
}

func (config *AnalysisConfig) SetInValid() {
	C.PD_SetInValid(config.c)
}

func (config *AnalysisConfig) IsValid() bool {
	return ConvertCBooleanToGo(C.PD_IsValid(config.c))
}