or run

npx @tessl/cli init
Log in

Version

Tile

Overview

Evals

Files

Files

docs

configuration.mddataset.mddistributed.mdfileio.mdindex.mdlogging.mdmodels.mdoptimization.mdregistry.mdtraining.mdvisualization.md

configuration.mddocs/

0

# Configuration Management

1

2

Advanced configuration system supporting Python-style and plain-text configuration files with inheritance, variable interpolation, and runtime modification capabilities. The system enables hierarchical configuration management essential for complex training pipelines.

3

4

## Capabilities

5

6

### Config Class

7

8

Main configuration class for loading and managing configuration files with support for inheritance, variable interpolation, and runtime modification.

9

10

```python { .api }

11

class Config:

12

def __init__(self, cfg_dict: dict = None, cfg_text: str = None, filename: str = None):

13

"""

14

Initialize a Config object.

15

16

Parameters:

17

- cfg_dict: Dictionary containing configuration data

18

- cfg_text: String containing configuration text

19

- filename: Path to configuration file

20

"""

21

22

@staticmethod

23

def fromfile(filename: str, use_predefined_variables: bool = True, import_custom_modules: bool = True) -> 'Config':

24

"""

25

Load configuration from file.

26

27

Parameters:

28

- filename: Path to configuration file (.py or .json)

29

- use_predefined_variables: Whether to use predefined variables

30

- import_custom_modules: Whether to import custom modules

31

32

Returns:

33

Config object loaded from file

34

"""

35

36

def merge_from_dict(self, options: dict, allow_list_keys: bool = True):

37

"""

38

Merge configuration from dictionary.

39

40

Parameters:

41

- options: Dictionary of options to merge

42

- allow_list_keys: Whether to allow list-type keys

43

"""

44

45

def dump(self, file: str = None) -> str:

46

"""

47

Dump configuration to string or file.

48

49

Parameters:

50

- file: Optional file path to save configuration

51

52

Returns:

53

Configuration as string

54

"""

55

56

def pretty_text(self) -> str:

57

"""

58

Get pretty formatted configuration text.

59

60

Returns:

61

Formatted configuration string

62

"""

63

64

@property

65

def filename(self) -> str:

66

"""Get the filename of the configuration."""

67

68

@property

69

def text(self) -> str:

70

"""Get the text content of the configuration."""

71

```

72

73

### ConfigDict Class

74

75

Dictionary-like configuration object with attribute access support, enabling convenient access to configuration values using dot notation.

76

77

```python { .api }

78

class ConfigDict(dict):

79

def __init__(self, *args, **kwargs):

80

"""Initialize ConfigDict with dictionary functionality plus attribute access."""

81

82

def __getattr__(self, name: str):

83

"""

84

Get configuration value as attribute.

85

86

Parameters:

87

- name: Attribute name

88

89

Returns:

90

Configuration value

91

"""

92

93

def __setattr__(self, name: str, value):

94

"""

95

Set configuration value as attribute.

96

97

Parameters:

98

- name: Attribute name

99

- value: Value to set

100

"""

101

102

def __delattr__(self, name: str):

103

"""

104

Delete configuration attribute.

105

106

Parameters:

107

- name: Attribute name to delete

108

"""

109

110

def to_dict(self) -> dict:

111

"""

112

Convert ConfigDict to regular dictionary.

113

114

Returns:

115

Regular dictionary representation

116

"""

117

```

118

119

### Argument Parsing Integration

120

121

Support for integrating configuration with command-line argument parsing through argparse actions.

122

123

```python { .api }

124

class DictAction:

125

def __init__(self, option_strings, dest, nargs=None, const=None, default=None, type=None, choices=None, required=False, help=None, metavar=None):

126

"""

127

Argparse action for handling dictionary arguments.

128

129

Parameters:

130

- option_strings: Option strings for the argument

131

- dest: Destination attribute name

132

- nargs: Number of arguments

133

- const: Constant value

134

- default: Default value

135

- type: Argument type

136

- choices: Valid choices

137

- required: Whether argument is required

138

- help: Help text

139

- metavar: Metavar for help

140

"""

141

142

def __call__(self, parser, namespace, values, option_string=None):

143

"""

144

Process the argument values.

145

146

Parameters:

147

- parser: ArgumentParser instance

148

- namespace: Namespace object

149

- values: Argument values

150

- option_string: Option string used

151

"""

152

```

153

154

### Configuration Utilities

155

156

Utility functions for reading and processing configuration files with support for base configurations and inheritance.

157

158

```python { .api }

159

def read_base(filename: str, scope: str = None) -> dict:

160

"""

161

Read base configuration file.

162

163

Parameters:

164

- filename: Path to base configuration file

165

- scope: Scope for configuration variables

166

167

Returns:

168

Dictionary containing configuration data

169

"""

170

```

171

172

## Usage Examples

173

174

### Basic Configuration Loading

175

176

```python

177

from mmengine import Config

178

179

# Load from Python configuration file

180

cfg = Config.fromfile('config.py')

181

182

# Access configuration values

183

print(cfg.model.type)

184

print(cfg.dataset.batch_size)

185

186

# Load from JSON file

187

cfg = Config.fromfile('config.json')

188

```

189

190

### Configuration Inheritance

191

192

```python

193

# config.py with base configuration

194

_base_ = './base_config.py'

195

196

# Override specific values

197

model = dict(

198

type='ResNet',

199

depth=50,

200

num_classes=1000

201

)

202

203

# Load configuration with inheritance

204

cfg = Config.fromfile('config.py')

205

```

206

207

### Runtime Configuration Modification

208

209

```python

210

from mmengine import Config

211

212

cfg = Config.fromfile('config.py')

213

214

# Merge from dictionary

215

cfg.merge_from_dict({

216

'optimizer.lr': 0.001,

217

'dataset.batch_size': 32

218

})

219

220

# Using ConfigDict for attribute access

221

cfg_dict = cfg.get('model', ConfigDict())

222

cfg_dict.num_classes = 10

223

```

224

225

### Command Line Integration

226

227

```python

228

import argparse

229

from mmengine import Config, DictAction

230

231

parser = argparse.ArgumentParser()

232

parser.add_argument('config', help='config file')

233

parser.add_argument('--cfg-options', action=DictAction, help='override config options')

234

235

args = parser.parse_args()

236

cfg = Config.fromfile(args.config)

237

238

if args.cfg_options:

239

cfg.merge_from_dict(args.cfg_options)

240

```